<?php
    

?>
<div class="hidden" id="addReason-cont">
    <div class="well well-small objCell-info">
        <h4 class="pull-right" id="addReason-date"></h4>
        <h4 id="addReason-obj_name"></h4>
        <div style="margin:10px 0px 5px; text-align: justify;"><div id="addReason-address"></div></div>

        
        <form class="form-inline well well-large" action="" method="POST" name="addReasonForm" id="addReasonForm" style="border-width: 0px;">
            <fieldset>

                <input type="hidden" name="action" id="addReason-action" value="">
                <input type="hidden" name="id" id="addReason-id" value="">
                <input type="hidden" name="to_date" id="addReason-to_date" value="">
                <input type="hidden" name="trc_id" id="addReason-trc_id" value="">

                <div class="control-group">
                    <label class="control-label" for="reason">Причина отказа</label>
                    <div class="controls">
                        <input type="text" class="input-xlarge" id="addReason-reason" name="reason" value="">
                    </div>
                </div>

                <div class="control-group">
                    <label class="control-label" for="manual_value">Значение (ручной ввод)</label>
                    <div class="controls">
                        <input type="text" class="input-xlarge" id="addReason-manual_value" name="manual_value" value="">
                    </div>
                </div>

                <div class="form-actions">
                    <button type="button" class="btn btn-primary" id="addReason-submit"><i class="icon-ok icon-white"></i> Сохранить</button>
                    &nbsp;
                    <button type="button" class="btn btn-inverse" id="addReason-delete"><i class="icon-minus-sign icon-white"></i> Удалить</button>
                    <button type="button" class="btn btn-danger" id="addReason-cancel"><i class="icon-remove icon-white"></i> Закрыть</button>
                </div>
            </fieldset>
        </form>

    </div>
</div>    



<script>

    function addReasonDialogShow() {
        Elm.addReasonCont.dialog('open');
    }

    function addReasonDialogHide() {
        Elm.addReasonCont.dialog('close');
    }
    
    function addReasonDialogClose() {
        Elm.addReasonForm.submitCallback = function(data) {};
        Elm.addReasonForm.cancelCallback = function(data) {};
        Elm.addReasonForm.deleteCallback = function(data) {};
        Elm.addReasonForm.currentVal = {};
        addReasonDialogHide();
        return false;
    }
    
    
    function addReasonDialogOpen(params, onSubmit, onCancel, onDelete) {
        
        Elm.addReasonForm.submitCallback = onSubmit || function(){};
        Elm.addReasonForm.cancelCallback = onCancel || function(){};
        Elm.addReasonForm.deleteCallback = onDelete || function(){};
        
        Elm.addReasonForm.currentVal = {};
        
        // сорри за говнокод =) эта форма не автоматизируется
        Elm.addReasonForm.currentVal.id = arrayGetValue(params, 'id', 0);
        if (Elm.addReasonForm.currentVal.id*1 > 0) Elm.addReasonForm.currentVal.action = '<?php echo ACTIONS::REASON_UPD; ?>';
        else Elm.addReasonForm.currentVal.action = '<?php echo ACTIONS::REASON_ADD; ?>';
        Elm.addReasonForm.currentVal.trc_id = arrayGetValue(params, 'trc_id', '0');
        Elm.addReasonForm.currentVal.to_date = arrayGetValue(params, 'to_date', '');
        Elm.addReasonForm.currentVal.reason = arrayGetValue(params, 'reason', '');
        Elm.addReasonForm.currentVal.manual_value = arrayGetValue(params, 'manual_value', '');
        
        Elm.addReasonForm.currentVal.address = arrayGetValue(params, 'address', 'Адрес не указан');
        Elm.addReasonForm.currentVal.obj_name = arrayGetValue(params, 'obj_name', '???');
        
        addReasonFormFill();
        addReasonDialogShow();
    }
    
    function addReasonFormFill() {
        $('#addReason-date').html(Elm.addReasonForm.currentVal.to_date);
        $('#addReason-obj_name').html(Elm.addReasonForm.currentVal.obj_name);
        $('#addReason-address').html(Elm.addReasonForm.currentVal.address);
        
        $('#addReason-action').val(Elm.addReasonForm.currentVal.action);
        $('#addReason-id').val(Elm.addReasonForm.currentVal.id);
        $('#addReason-to_date').val(Elm.addReasonForm.currentVal.to_date);
        $('#addReason-trc_id').val(Elm.addReasonForm.currentVal.trc_id);
        $('#addReason-reason').val(Elm.addReasonForm.currentVal.reason);
        $('#addReason-manual_value').val(Elm.addReasonForm.currentVal.manual_value);
        
        if (Elm.addReasonForm.currentVal.id*1 > 0) $('#addReason-delete').show();
        else $('#addReason-delete').hide();
    }
    
    
    
    
    function addReasonFormDeleteItem() {
        var send = {
            id: Elm.addReasonForm.currentVal.id*1,
            action: '<?php echo ACTIONS::REASON_DEL; ?>'
        };
        
        
        jsonRequest(
            'reasons.php',
            send,
            function(data) {
                if(!checkSession(data)) {
                    Elm.loginFormModal.dialog('open');
                    Elm.loginFormModal.successCallback = function(){ $('#addReason-delete').click(); };
                    return;
                }

                if (!data.state) {
                    uiAlert(data.message,'Error');
                    return;
                }

                uiAlert('Запись удалена','Информация');
                
                if(typeof(Elm.addReasonForm.deleteCallback) == 'function') Elm.addReasonForm.deleteCallback();
                addReasonDialogClose();
            },

            function() {
                uiAlert('Ошибка соединения','Error');
                reasonListFormEnable();
            }
        );
        
    }


    $(function() {
        Elm.addReasonCont = $('#addReason-cont');
        Elm.addReasonForm = $('#addReasonForm');
        // Коллбэки см в addReasonDialogClose()
        

        

        Elm.addReasonCont.dialog({
            width       : 450,
            modal       : true,
            resizable   : false,
            title       : "Редактировать отказ",
            autoOpen    : false,
            buttons     : {}
        }).removeClass('hidden');
        Elm.addReasonCont.dialog('widget').find('.ui-dialog-titlebar-close').hide();
        
        
        addReasonDialogClose();
        

        $('#addReason-cancel').click(function(){
            if (typeof(Elm.addReasonForm.cancelCallback) == 'function') Elm.addReasonForm.cancelCallback();
            addReasonDialogClose();
            return false;
        });
        
        
        $('#addReason-submit').click(function(){
            if (typeof(Elm.addReasonForm.submitCallback) == 'function') {
                Elm.addReasonForm.currentVal.reason = $('#addReason-reason').val();
                Elm.addReasonForm.currentVal.manual_value = $('#addReason-manual_value').val();
                
                var val = Elm.addReasonForm.currentVal.manual_value;
                if (val && /[^0-9.,]/gi.test(val)) {
                    uiAlert('Некорректное значение','Error');
                    return;
                }
                Elm.addReasonForm.currentVal.manual_value = val.gReplace(',', '.');
                
                Elm.addReasonForm.submitCallback( $.extend({}, Elm.addReasonForm.currentVal) );
            }
            return false;
        });
        
        $('#addReason-delete').click(function() {
            uiConfirm({
                title : 'Подтверждение',
                text :  'Удалить эту запись об отказе?',
                ok :    function(){ addReasonFormDeleteItem(); },
                cancel: function(){}
            });
        });
        
        
        var clearByChange = [];
        clearByChange['addReason-reason'] = $('#addReason-manual_value');
        clearByChange['addReason-manual_value'] = $('#addReason-reason');

        Elm.addReasonForm.find('input').keydown(function(){
            if (clearByChange.hasOwnProperty(this.id)) clearByChange[this.id].val('');
        });


    });




</script>